﻿Imports MapWinGIS
Imports System.ComponentModel
Imports System.Windows.Forms
<Serializable()> Public Class ESMLineLayer
    Inherits EditableShapeLayer




    Public Sub New(ByVal ln As String, ByVal tl As BaseTool)


        MyBase.New(ln, tl)

        linetype = FtkLineStipple.lsNone
        lineColor = Drawing.Color.Black
        lineWidth = 1

    End Sub

    '这些都是为了讨厌的VB事件逻辑啊
    Protected Sub New( _
      ByVal info As System.Runtime.Serialization.SerializationInfo, _
      ByVal context As System.Runtime.Serialization.StreamingContext)

        MyBase.New(info, context)

    End Sub




    '线条相关以及对应的处理程序

    Protected linetype As FtkLineStipple

    <CategoryAttribute("外观设置"), _
   DisplayName("线条类型")> _
   Public Overridable Property LinetypeProperty() As FtkLineStipple
        Get
            Return linetype
        End Get
        Set(ByVal value As FtkLineStipple)
            linetype = value

            Me.OnLnTypeChanged(linetype)

        End Set
    End Property


    Protected lineColor As Drawing.Color
    <CategoryAttribute("外观设置"), _
    DisplayName("线条颜色")> _
    Public Overridable Property LineColorProperty() As Drawing.Color
        Get
            Return lineColor
        End Get
        Set(ByVal value As Drawing.Color)
            lineColor = value
            Me.OnLnClChanged(lineColor)
        End Set
    End Property
    Protected lineWidth As Double

    <CategoryAttribute("外观设置"), _
   DisplayName("线条宽度")> _
   Public Overridable Property LineWidthProperty() As Double
        Get
            Return lineWidth
        End Get
        Set(ByVal value As Double)
            lineWidth = value

            Me.OnLnWdChanged(lineWidth)

        End Set
    End Property

    Private Sub OnLnWdChanged(ByVal d As Double)

        mapwin.set_ShapeLayerLineWidth(m_iLayer, d)
    End Sub
    Private Sub OnLnClChanged(ByVal cl As Drawing.Color)

        Dim ucl As UInt32 = RGB(cl.R, cl.G, cl.B)
        mapwin.set_ShapeLayerLineColor(m_iLayer, ucl)
    End Sub
    Private Sub OnLnTypeChanged(ByVal lt As tkLineStipple)

        Dim temp As MapWinGIS.tkLineStipple = Me.getLineType(lt)
        mapwin.set_ShapeLayerLineStipple(m_iLayer, temp)


    End Sub

    Public Overrides Function AddEntity(ByVal sn As String, ByVal ptlist() As MapWinGIS.Point, ByVal desc As String) As Integer


        If ptlist.Length < 2 Then
            Return -1
        End If

        '关键节点，呵呵
        Dim en As New FEntity(sn, ptlist, desc)
        dicKeyPts.Add(sn, en)




        Dim sfPoly As Shapefile = mapwin.get_GetObject(m_iLayer)




        sfPoly.StartEditingShapes(True)





        Dim shp As New Shape
        shp.ShapeType = ShpfileType.SHP_POLYLINE
        Dim ptIndex As Integer = 0

        Dim n As Integer = ptlist.Length



        For i As Integer = 1 To n

            shp.InsertPoint(ptlist(i - 1), ptIndex)


        Next




        Dim shpIndex As Integer = sfPoly.NumShapes

        sfPoly.EditInsertShape(shp, shpIndex)

        sfPoly.EditCellValue(0, shpIndex, sn)
        sfPoly.EditCellValue(1, shpIndex, desc)

        sfPoly.StopEditingShapes()





        Me.DrawText()


        mapwin.Redraw()

        Return shpIndex

    End Function

    Public Overrides Sub AddEntityRange(ByVal lst As System.Collections.Generic.List(Of FEntity))

        For Each en As FEntity In lst

            dicKeyPts.Add(en.sn, en)
        Next





        Dim ptlist As MapWinGIS.Point()
        Dim sn As String
        Dim desc As String

        Dim sfPoly As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)



        sfPoly.StartEditingShapes(True)



        For Each en As FEntity In lst



            sn = en.sn
            ptlist = en.ptlist
            desc = en.desc


            If ptlist.Length < 2 Then

            Else


                Dim shp As New Shape
                shp.ShapeType = ShpfileType.SHP_POLYLINE
                Dim ptIndex As Integer = 0

                Dim n As Integer = ptlist.Length



                For i As Integer = 1 To n

                    shp.InsertPoint(ptlist(i - 1), ptIndex)


                Next




                Dim shpIndex As Integer = sfPoly.NumShapes

                sfPoly.EditInsertShape(shp, shpIndex)

                sfPoly.EditCellValue(0, shpIndex, sn)
                sfPoly.EditCellValue(1, shpIndex, desc)





            End If





        Next

        sfPoly.StopEditingShapes()


        '由于新增加了对象，所以需要根据现有状态，对对象操作

        Me.DrawText()

        mapwin.Redraw()

    End Sub



    Public Overrides Sub InitNewLayer(ByRef m As AxMapWinGIS.AxMap, ByVal OutPath As String)


        Dim sf As New Shapefile
        Dim strPath(2) As String


        'strPath(0) = "C:\temp\" + lname + ".shp"
        'strPath(1) = "C:\temp\" + lname + ".dbf"
        'strPath(2) = "C:\temp\" + lname + ".shx"

        'If System.IO.File.Exists(strPath(0)) Then
        '    System.IO.File.Delete(strPath(0))
        '    System.IO.File.Delete(strPath(1))
        '    System.IO.File.Delete(strPath(2))

        'End If




        sf.CreateNew("", MapWinGIS.ShpfileType.SHP_POLYLINE)




        Dim f As New Field

        f = New Field
        f.Name = "Name"
        f.Type = FieldType.STRING_FIELD
        f.Width = 50

        Dim fd As New Field

        fd = New Field
        fd.Name = "desc"
        fd.Type = FieldType.STRING_FIELD
        fd.Width = 50


        sf.StartEditingShapes(True)
        sf.EditInsertField(f, 0)
        sf.EditInsertField(fd, 1)

        sf.StopEditingShapes(True, True)

      

       

        '加入载入的逻辑

        Dim strSave As String = OutPath + lname + ".shp"

        strPath(0) = OutPath + lname + ".shp"
        strPath(1) = OutPath + lname + ".dbf"
        strPath(2) = OutPath + lname + ".shx"

        For i As Integer = 0 To strPath.Length - 1
            If System.IO.File.Exists(strPath(i)) Then
                Try
                    System.IO.File.SetAttributes(strPath(i), IO.FileAttributes.Normal)
                    System.IO.File.Delete(strPath(i))
                Catch ex As Exception

                End Try

            End If
        Next


        Dim bS As Boolean = sf.SaveAs(strSave)

        If Not bS Then
            MessageBox.Show(lname + "文件创建失败！，请查看电脑软件使用权限")

        End If

        fname = strSave


        Me.InitLayer(m)

    End Sub

    Public Overrides Sub InitProp()

        Me.OnLnClChanged(lineColor)
        Me.OnLnWdChanged(lineWidth)
        Me.OnLnTypeChanged(linetype)



    End Sub
End Class
